package org.example.TOOL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.DatabaseMetaData;
import java.util.ArrayList;
import java.util.List;


public class main1 {

    public static void main(String[] args) {
        // 数据库连接信息
        String dbUrl = "jdbc:mysql://10.10.1.8:3306/edubigdata?useUnicode=true&characterEncoding=utf-8&useSSL=false";
        String username = "edubigdata";
        String password = "4v&GUwYe@1Tr";
        String[] tables = {"pd_cbjczl_xx", "pd_gccch_xx", "pd_gcctd_xx", "pd_jxsp_jxcgj_xx", "pd_jxsp_jyjxyjyggxm_xx", "pd_jxsp_pxjxqk_xx", "pd_jyhd_cjjskyqk_xx", "pd_kysp_fblwqk_xx", "pd_kysp_jskycgzhqk_xx", "pd_kysp_zlzz_xx", "pd_sxjd_jxxmjdqk_xx", "pd_sxjd_sxsjsxjdqk_xx", "pd_xxcx_czbyzp_xx", "pd_xxcx_fblwqk_xx", "pd_xxcx_jsqk_xx", "pd_xxcx_yszybs_xx", "pd_xxcx_zlzzq_xx", "pd_ylkcjs_xx", "pd_ysylzy_xx", "pd_jydh_cjcxcyxljh_xx", "pd_kysp_zckyxm_xx"};

        // 需要获取字段名的表名数组

        List<String> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        int num = 0;

        int num1 = 0;

        // 遍历表名并获取字段名
        try (Connection connection = DriverManager.getConnection(dbUrl, username, password)) {
            DatabaseMetaData metaData = connection.getMetaData();

            for (String table : tables) {
                System.out.println("表名: " + table);
                System.out.println("字段名列表:");

                // 获取表的列信息
                try (ResultSet columns = metaData.getColumns(null, null, table, null)) {

                    boolean code = false;
                    boolean xnzymc = false;
                    while (columns.next()) {
                        String columnName = columns.getString("COLUMN_NAME"); // 列名
                        String columnType = columns.getString("TYPE_NAME");  // 列类型
                        int columnSize = columns.getInt("COLUMN_SIZE");      // 列大小

                        // 打印字段信息
                        System.out.printf("  字段名: %s, 类型: %s(%d)%n", columnName, columnType, columnSize);

                        //大小字母不分比较
                        if (columnName.equals("code") ) {
                            code = true;
                            num++;
                        }
                        if (columnName.equals("zymc") ) {
                            xnzymc = true;
                            num1++;
                        }
                    }
                    System.out.println("code:" + code + "    xnzymc:" + xnzymc);
                    System.out.println();
                    if ( xnzymc) {
                        list.add(table);
                    } else {
                        list1.add(table);
                    }
                }

                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        System.out.println(num);
        System.out.println(num1);
        System.out.println("code   xnzymc   字段存在视图表");
        System.out.println(list.toString());
        System.out.println("code   xnzymc   字段不存在视图表");
        System.out.println(list1.toString());
    }


}
